#[derive(FromForm)]
struct WsForm {
    skip_number: usize
}

#[derive(Serialize)]
struct WsCopywrite {
    id: usize,
    title: String
}

#[derive(Serialize)]
struct WsClip {
    id: usize,
    title: String
}

#[derive(Serialize)]
struct WsExamineList {
    copywrite: Vec<WsCopywrite>,
    clip: Vec<WsClip>
}

#[post("/ws_lib/examine", data="<form>")]
fn ws_examine(form: Form<WsForm>, state: State<MySqlDriver>) -> Json<Value> {
    let mut examine_list: Vec<WsExamineList> = Vec::new();
    let pool_result: Result<Pool, mysql::Error> = Pool::new(state.driver.as_str());
    let pool: Pool;
    if let Ok(p) = pool_result {
        pool = p;
    } else {
        return Json(to_value(examine_list).unwrap());
    }
    let conn_result: Result<PooledConn, mysql::Error> = pool.get_conn();
    let mut conn: PooledConn;
    if let Ok(c) = conn_result {
        conn = c;
    } else {
        return Json(to_value(examine_list).unwrap());
    }

    let mut sql: String = format!("SELECT `id`, `title` FROM `copywrite_order` WHERE `status` = 2 ORDER BY `id` ASC LIMIT {}, 10", form.skip_number);
    let rows: Vec<Row> = conn.query(sql).unwrap();
    let mut copywrite_list: Vec<WsCopywrite> = Vec::new();
    for row in rows {
		let (id, title): (usize, String) = from_row(row);
		copywrite_list.push(WsCopywrite {
			id: id,
			title: title
        });
    }
    sql = format!("SELECT `clip_o`.`id`, `copy_o`.`title` FROM `clip_order` AS `clip_o` INNER JOIN `copywrite_order` AS `copy_o` ON `clip_o`.`copywrite_id` = `copy_o`.`id` WHERE `clip_o`.`status` = 2 ORDER BY `clip_o`.`id` ASC LIMIT {}, 10", form.skip_number);
    let rows: Vec<Row> = conn.query(sql).unwrap();
    let mut clip_list: Vec<WsClip> = Vec::new();
    for row in rows {
		let (id, title): (usize, String) = from_row(row);
		clip_list.push(WsClip {
			id: id,
			title: title
        });
    }
    examine_list.push(WsExamineList {
        copywrite: copywrite_list,
        clip: clip_list
    });
    return Json(to_value(examine_list).unwrap());
}

